package com.vhly.ddxdecompiler;

import com.vhly.classfile.JavaClass;
import com.vhly.ddxdecompiler.dexreader.DexReader;
import com.vhly.ddxdecompiler.dexreader.file.ClassDefsSection;
import com.vhly.ddxdecompiler.dexreader.file.DexFile;
import com.vhly.ddxdecompiler.dexreader.file.mixeditem.ClassDefItem;
import com.vhly.ddxdecompiler.dexreader.util.StreamUtil;

import java.io.*;

/**
 * Created by IntelliJ IDEA.
 * User: vhly
 * Date: 2009-9-23
 * Time: 14:56:01
 *
 * @author vhly[FR]
 */
public class Main {
    public static void main(String[] args) {
        DexFile dexFile;
        FileInputStream fin = null;

        int argc = args.length;
        if (argc == 2) {
            try {
                fin = new FileInputStream(args[0]);
                dexFile = DexReader.parse(fin);
                ClassDefsSection cls = dexFile.getClassDefs();
                int size = cls.size();
                ClassDefItem ci;
                JavaClass clazz;
                File targetDir = new File(args[1]);
                for (int i = 0; i < size; i++) {
                    ci = cls.indexOf(i);
                    clazz = ci.reverseClass();
                    writeToJava(clazz,targetDir);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fin != null) {
                    try {
                        fin.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }else{
            System.out.println("DexDecompiler by vhly[FR] 2010");
            System.out.println("Usage: java -cp dexdecompiler.jar;ClassFile.jar com.vhly.ddxdecompiler.Main <dexfile> <class folder>");
        }
    }

    /**
     * Write JavaClass Object to folder, this method will write class file to full path,<br/>
     * if class name is com.vhly.test.T1, and targetDir is clazz folder, it will create clazz/com/vhly/test/T1.class file.
     * @param clazz
     * @param targetDir
     */
    private static void writeToJava(JavaClass clazz, File targetDir) {
        if (clazz == null)
            return;
        String cn = clazz.getThisName();
        if(cn.startsWith("L")){
            cn = cn.substring(1);
        }
        if(cn.endsWith(";")){
            cn = cn.substring(0,cn.length() - 1);
        }
        cn = cn + ".class";
        System.out.println("cn = " + cn);
        int index = cn.lastIndexOf("/");
        if(index != -1){
            String td = cn.substring(0,index);
            File tdir = new File(targetDir,td);
            tdir.mkdirs();
        }
        File target = new File(targetDir,cn);
        FileOutputStream fout = null;
        DataOutputStream dout = null;
        try {
            fout = new FileOutputStream(target);
            dout = new DataOutputStream(fout);
            clazz.write(dout);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            StreamUtil.close(dout);
            StreamUtil.close(fout);
        }
    }
}
